﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BitcoinCommon;
using BitcoinExchange;
using BitcoinMarket;
using BitcoinTrading.Data;

namespace BitcoinTrading.Trading
{
    public class ActiveTrader
    {
        double MarginalBuy = -1500.0;
        double MarginalSell = 6000.0;

        MainInfo _info;
        DataController _data;
        Exchange _exchange;

        public ActiveTrader(MainInfo info, Exchange exchange, DataController data)
        {
            _info = info;
            _exchange = exchange;
            _data = data;
        }

        public double GetMarginalBuy()
        {
            return MarginalBuy;
        }

        public double GetMarginalSell()
        {
            return MarginalSell;
        }

        public void ExecuteTrade()
        {
            if (!_info.IsLogin())
                return;

            MarginalProfit margin = _data.MarginalProfitData(eCalcMarginTable.BUY);

            if (margin._dbEstProfit > MarginalBuy)
            {
                if (margin._dbKRWPrice == 0.0 || margin._dbUSDPrice == 0.0)
                    return;

                TradeMarginalBuy("2.0", margin._dbKRWPrice.ToString(), margin._dbUSDPrice.ToString());
            }


            margin = _data.MarginalProfitData(eCalcMarginTable.SELL);

            if (margin._dbEstProfit > MarginalSell)
            {
                if (margin._dbKRWPrice == 0.0 || margin._dbUSDPrice == 0.0)
                    return;

                TradeMarginalSell("2.0", margin._dbKRWPrice.ToString(), margin._dbUSDPrice.ToString());
            }  
        }

        public void SetMarginalPrice(double dbBuyPrice, double dbSellPrice)
        {
            MarginalBuy = dbBuyPrice;
            MarginalSell = dbSellPrice;
        }

        public bool TradeMarginalBuy()
        {
            bool bReturn = false;

            double dbBTCAmount = 1.0;
            double dbKRWBTCPrice = Double.Parse(_data.KorbitBestAsk());
            double dbUSDBTCPrice = Double.Parse(_data.BTCeBestBid());

            if (!_info.IsLogin())
                return bReturn;

            if (!IsTradable(eCalcMarginTable.BUY, dbBTCAmount, dbKRWBTCPrice, dbUSDBTCPrice))
                return bReturn;

            _exchange.KorbitOrderBuy(dbKRWBTCPrice.ToString(), dbBTCAmount.ToString());
            _exchange.BTCeOrderSell(dbUSDBTCPrice.ToString(), (dbBTCAmount * 1.002).ToString());
            
            return true;
        }

        public bool TradeMarginalSell()
        {
            bool bReturn = false;

            double dbBTCAmount = 1.0;
            double dbKRWBTCPrice = Double.Parse(_data.KorbitBestBid());
            double dbUSDBTCPrice = Double.Parse(_data.BTCeBestAsk());

            if (!_info.IsLogin())
                return bReturn;

            if (!IsTradable(eCalcMarginTable.SELL, dbBTCAmount, dbKRWBTCPrice, dbUSDBTCPrice))
                return bReturn;

            _exchange.KorbitOrderSell(dbKRWBTCPrice.ToString(), dbBTCAmount.ToString());
            _exchange.BTCeOrderBuy(dbUSDBTCPrice.ToString(), (dbBTCAmount * 1.002).ToString());

            return true;
        }

        public void TradeMarginalBuy(string strBTCAmount, string strKRWBTCPrice, string strUSDBTCPrice)
        {
            double dbBTCAmount = Double.Parse(strBTCAmount);
            double dbKRWBTCPrice = Double.Parse(strKRWBTCPrice);
            double dbUSDBTCPrice = Double.Parse(strUSDBTCPrice);

            if (!_info.IsLogin())
                return;

            if (!IsTradable(eCalcMarginTable.BUY, dbBTCAmount, dbKRWBTCPrice, dbUSDBTCPrice))
                return;
            
            _exchange.KorbitOrderBuy(dbKRWBTCPrice.ToString(), dbBTCAmount.ToString());
            _exchange.BTCeOrderSell(dbUSDBTCPrice.ToString(), (dbBTCAmount * 1.002).ToString());
        }

        public void TradeMarginalSell(string strBTCAmount, string strKRWBTCPrice, string strUSDBTCPrice)
        {
            double dbBTCAmount = Double.Parse(strBTCAmount);
            double dbKRWBTCPrice = Double.Parse(strKRWBTCPrice);
            double dbUSDBTCPrice = Double.Parse(strUSDBTCPrice);

            if (!_info.IsLogin())
                return;

            if (!IsTradable(eCalcMarginTable.SELL, dbBTCAmount, dbKRWBTCPrice, dbUSDBTCPrice))
                return;

            _exchange.KorbitOrderSell(dbKRWBTCPrice.ToString(), dbBTCAmount.ToString());
            _exchange.BTCeOrderBuy(dbUSDBTCPrice.ToString(), (dbBTCAmount * 1.002).ToString());
        }

        public bool IsTradable(eCalcMarginTable eCalc, double dbBTCAmount, double dbKRWBTCPrice, double dbUSDBTCPrice)
        {
            bool bReturn = false;

            double dbKRWCurrency = _exchange.KRWAmount();
            double dbKRWBTC = _exchange.KorbitBTCAmount();
            double dbUSDCurrency = _exchange.USDAmount();
            double dbUSDBTC = _exchange.BTCeBTCAmount();

            double dbSellBTCAmount;
            double dbBuyBTCAmount;

            if (eCalc == eCalcMarginTable.BUY)
            {
                dbBuyBTCAmount = dbKRWCurrency / dbKRWBTCPrice;
                dbSellBTCAmount = dbUSDBTC * 0.998;
            }
            else
            {
                dbBuyBTCAmount = dbUSDCurrency / dbUSDBTCPrice;
                dbSellBTCAmount = dbKRWBTC;
            }

            if (dbBuyBTCAmount > dbBTCAmount && dbSellBTCAmount > dbBTCAmount)
                bReturn = true;

            return bReturn;
        }
    }
}
